﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MPMovieCatalog.Domain;

namespace MPMovieCatalog
{
    public partial class EditForm : Form
    {
        private long movieId = -1;

        public EditForm()
        {
            InitializeComponent();
        }

        public MovieInfo Data
        {
            get
            {
                MovieInfo movieInfo = new MovieInfo();
                movieInfo.Id = movieId;

                movieInfo.Name = titleTextBox.Text;
                movieInfo.Tagline = taglineTextBox.Text;
                movieInfo.Director = directorTextBox.Text;
                movieInfo.Writer = writerTextBox.Text;
                movieInfo.Year = yearUpDown.Value.ToString();
                movieInfo.Runtime = runtimeUpDown.Value.ToString();
                movieInfo.IMDBRating = imdbRatingTextBox.Text;
                movieInfo.IMDBNumber = imdbNumberTextBox.Text;
                movieInfo.IMDBVotes = imdbVotesTextBox.Text;
                movieInfo.MPAARating = mpaaRatingComboBox.Text;

                movieInfo.Description = descriptionTextBox.Text;

                movieInfo.Picture = pictureFileTextBox.Text;

                foreach (Genre genre in genresCheckedListBox.CheckedItems)
                {
                    movieInfo.AddGenre(genre);
                }

                foreach (Actor actor in movieActorsListBox.Items)
                {
                    movieInfo.AddActor(actor);
                }

                return movieInfo;
            }
            set
            {
                movieId = value.Id;

                titleTextBox.Text = value.Name;
                taglineTextBox.Text = value.Tagline;
                directorTextBox.Text = value.Director;
                writerTextBox.Text = value.Writer;
                yearUpDown.Value = decimal.Parse(value.Year);
                runtimeUpDown.Value = decimal.Parse(value.Runtime);
                imdbRatingTextBox.Text = value.IMDBRating;
                imdbNumberTextBox.Text = value.IMDBNumber;
                imdbVotesTextBox.Text = value.IMDBVotes;
                mpaaRatingComboBox.Text = value.MPAARating;

                descriptionTextBox.Text = value.Description;

                pictureFileTextBox.Text = value.Picture;
                pictureBox.ImageLocation = value.Picture;

                value.Genres.Sort(new Comparer());
                foreach (Genre genre in value.Genres)
                {
                    genresCheckedListBox.Items.Add(genre, true);
                }

                AvailableGenres.Sort(new Comparer());
                foreach (Genre availGenre in AvailableGenres)
                {
                    genresCheckedListBox.Items.Add(availGenre);
                }

                foreach (Actor actor in value.Actors)
                {
                    movieActorsListBox.Items.Add(actor);
                }

                foreach (Actor availActor in AvailableActors)
                {
                    availableActorsListBox.Items.Add(availActor);
                }

            }
        }

        public ArrayList AvailableGenres { get; set; }

        public ArrayList AvailableActors { get; set; }

        private void browsePictureFileButton_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = pictureFileTextBox.Text;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                pictureBox.ImageLocation = openFileDialog.FileName;
                pictureFileTextBox.Text = openFileDialog.FileName;
            }
        }

        private void addGenreButton_Click(object sender, EventArgs e)
        {
            if (newGenreTextBox.Text != null && !"".Equals(newGenreTextBox.Text))
            {
                string name = newGenreTextBox.Text;
                int index = genresCheckedListBox.FindStringExact(name);
                if (index < 0)
                {
                    genresCheckedListBox.BeginUpdate();

                    List<Genre> checkedGenres = new List<Genre>();
                    List<Genre> uncheckedGenres = new List<Genre>();
                    for (int i = 0; i < genresCheckedListBox.Items.Count; i++)
                    {
                        Genre genre = (Genre)genresCheckedListBox.Items[i];
                        if (genresCheckedListBox.GetItemChecked(i))
                        {
                            checkedGenres.Add(genre);
                        }
                        else
                        {
                            uncheckedGenres.Add(genre);
                        }
                    }

                    checkedGenres.Sort(NamedEntity.CompareByName);

                    Genre newGenre = new Genre();
                    newGenre.Name = name;

                    uncheckedGenres.Add(newGenre);
                    uncheckedGenres.Sort(NamedEntity.CompareByName);

                    genresCheckedListBox.Items.Clear();

                    foreach (Genre checkedGenre in checkedGenres)
                    {
                        genresCheckedListBox.Items.Add(checkedGenre, true);
                    }

                    foreach (Genre uncheckedGenre in uncheckedGenres)
                    {
                        genresCheckedListBox.Items.Add(uncheckedGenre);
                    }


                    index = genresCheckedListBox.FindStringExact(name);
                    genresCheckedListBox.EndUpdate();
                }

                genresCheckedListBox.SelectedIndex = index;
                newGenreTextBox.Clear();
            }
        }

        private void mapActorButton_Click(object sender, EventArgs e)
        {
            if (availableActorsListBox.SelectedItems == null) return;
            foreach (Actor actor in availableActorsListBox.SelectedItems)
            {
                movieActorsListBox.Items.Add(actor);
            }

            for (int i = availableActorsListBox.SelectedItems.Count - 1; i >= 0; i--)
            {
                availableActorsListBox.Items.Remove(availableActorsListBox.SelectedItems[i]);
            }
        }

        private void unmapActorButton_Click(object sender, EventArgs e)
        {
            if (movieActorsListBox.SelectedItems == null) return;
            foreach (Actor actor in movieActorsListBox.SelectedItems)
            {
                availableActorsListBox.Items.Add(actor);
            }

            for (int i = movieActorsListBox.SelectedItems.Count - 1; i >= 0; i--)
            {
                movieActorsListBox.Items.Remove(movieActorsListBox.SelectedItems[i]);
            }
        }

        private void addActorButton_Click(object sender, EventArgs e)
        {
            if (newActorTextBox.Text != null && !"".Equals(newActorTextBox.Text))
            {
                string name = newActorTextBox.Text;
                int index = availableActorsListBox.FindStringExact(name);
                if (index < 0)
                {
                    Actor actor = new Actor();
                    actor.Name = name;

                    index = availableActorsListBox.Items.Add(actor);
                }
                availableActorsListBox.SelectedIndex = index;

                newActorTextBox.Clear();
            }
        }
    }

    public class Comparer : IComparer
    {

        #region IComparer Members

        public int Compare(object entity1, object entity2)
        {
            if (entity1 == null)
            {
                if (entity2 == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (entity2 == null)
                {
                    return 1;
                }
                else
                {
                    return CompareStrings(((NamedEntity) entity1).Name, ((NamedEntity) entity2).Name);
                }
            }
        }

        #endregion

        private int CompareStrings(string s1, string s2)
        {
            if (s1 == null)
            {
                if (s2 == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (s2 == null)
                {
                    return 1;
                }
                else
                {
                    return s1.CompareTo(s2);
                }
            }
        }

    }
}
